package com.example.server.web.domain.page;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.server.web.utils.MyObjectUtils;
import io.netty.util.internal.ObjectUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;

import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import java.util.List;
import java.util.Map;

import static org.springframework.beans.support.PagedListHolder.DEFAULT_PAGE_SIZE;

/**
 * 分页查询对象
 */
@ApiModel(value = "分页查询对象",description = "")
public class PageQueryDto {

    @ApiModelProperty(value = "页码",required = true)
    @Min(value = 1, message = "页号最小为1")
    private Integer page;
    @ApiModelProperty(value = "每页的记录大小",required = true)
    @Max(value = 50, message = "每页最大为50")
    private Integer limit;
    @ApiModelProperty("分页查询的参数")
    private Map<String, Object> params;

    /**
     * 当前记录起始索引 默认值
     */
    public static final int DEFAULT_PAGE_NUM = 1;

    /**
     * 每页显示记录数 默认值 默认查全部
     */
    public static final int DEFAULT_PAGE_SIZE = Integer.MAX_VALUE;


    public Object getParams(String param) {
        if(param==null){
            return null;
        }
        return  params.getOrDefault(param,null);
    }

    public void setParams(Map<String, Object> params) {
        this.params = params;
    }

    public Integer getPage() {
        return page;
    }

    public void setPage(Integer page) {
        this.page = page;
    }

    public Integer getLimit() {
        return limit;
    }

    public void setLimit(Integer limit) {
        this.limit = limit;
    }

    public <T> Page<T> build() {
        Integer pageNum = MyObjectUtils.defaultIfNull(getPage(), DEFAULT_PAGE_NUM);
        Integer pageSize = MyObjectUtils.defaultIfNull(getLimit(), DEFAULT_PAGE_SIZE);
        if (pageNum <= 0) {
            pageNum = DEFAULT_PAGE_NUM;
        }
        Page<T> page = new Page<>(pageNum, pageSize);
        return page;
    }

}
